package com.ruicar.afs.cloud.apply.pre.loan.mq.receiver;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.ruicar.afs.cloud.apply.business.service.ApplyRecordDetailsService;
import com.ruicar.afs.cloud.apply.common.entity.PreApproveInfo;
import com.ruicar.afs.cloud.apply.common.entity.WorkflowRecordDetails;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.approve.service.PreApproveService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.BusinessTypeEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.approveprev.ApprovePrevResultDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 处理结果接收MQ
 *
 * @author 耿继辉
 */
@Slf4j
@AllArgsConstructor
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class PreApproveDalProcessor implements AfsMqBizProcessor<ApprovePrevResultDto> {
    /**
     * 预审批service
     */
    private final PreApproveService preApproveService;
    private final ApplyRecordDetailsService applyRecordDetailsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(ApprovePrevResultDto entity) throws Exception {
        if (ObjectUtil.isNotNull(entity)) {
            //玄武征信结果返回
            if (StringUtil.isNotEmpty(entity.getApproveResult())) {
                PreApproveInfo byId = this.preApproveService.getById(entity.getApplyId());
                //新车:ABCD级
                if (BusinessTypeEnum.NEW_CAR.getCode().equals(byId.getBusinessType())) {
                    byId.setApplyStatus(entity.getApproveResult());
                    byId.setApproveDate(entity.getApproveDate());
                    this.preApproveService.updateById(byId);
                    log.info("新车预审批结果:{}", entity.getApproveResult() + "级");
                } else if (BusinessTypeEnum.OLD_CAR.getCode().equals(byId.getBusinessType())) {
                    //二手车:yes/no
                    if (WhetherEnum.YES.getCode().equals(entity.getApproveResult())) {
                        byId.setApplyStatus(ApplyConstants.PRE_APPROVE_PASS);
                        byId.setApproveDate(entity.getApproveDate());
                        this.preApproveService.updateById(byId);
                        log.info("二手车预审批结果:{}", entity.getApproveResult());
                    } else if (WhetherEnum.NO.getCode().equals(entity.getApproveResult())) {
                        byId.setApplyStatus(ApplyConstants.PRE_APPROVE_REFUSE);
                        byId.setApproveDate(entity.getApproveDate());
                        this.preApproveService.updateById(byId);
                        log.info("二手车预审批结果:{}", entity.getApproveResult());
                    }
                }
            } else if (StringUtil.isEmpty(entity.getApproveResult())) {
                //信审数据接收成功
                if (WhetherEnum.YES.getCode().equals(entity.getReceiveStatus())) {
                    log.info("信审接收数据成功:{}", entity.getApplyId());
                } else if (WhetherEnum.NO.getCode().equals(entity.getReceiveStatus())) {
                    //信审接收失败
                    log.info("信审接收数据失败:{}", entity.getApplyId());
                }
            }
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_CASE_APPLY_CTM_APPROVE_PREV_NOTICE;
    }

    @Override
    public boolean reQueueOnFail() {

        return false;
    }

}
